var express = require('express');
var router = express.Router();
let db = require('../tools/DB');
let to = require('../tools/to');




router.post('/', async (req, res) => {
    let {
        page = 1, pagesize = 9,keyword
    } = req.body;    
    let key1 = '%' + keyword + '%';
    let pageNum = (page - 1) * pagesize;
    
    let sql = `select id,cid,title,price,CONCAT('/uploads/course/',image_src) as image_src,type_name,grade_name,subject_name,area_name,category_name from e_course where title like '${key1}'  limit ?,?`;
    let sql2 = `select count(id) as totalNum from e_course where title like '${key1}'`;
  
    let [err, result] = await to(db.query(sql, [Number(pageNum),Number(pagesize)]));
    let [{totalNum}] = await db.query(sql2);
    let data = {
        page,
        pagesize,
        totalNum,
        result
    }   
    if(!err){
        res.json({code:200,msg:'成功',data})
    }else{
        res.json({code:200,msg:'失败',data:err})
    }
})


module.exports = router;